package top.mcwebsite.markdown

import androidx.compose.foundation.ScrollState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.UriHandler
import org.intellij.markdown.MarkdownElementTypes
import org.intellij.markdown.ast.ASTNode
import org.intellij.markdown.ast.getTextInNode

class MarkdownState(
    val onEvent: (MarkdownEvent) -> Unit,
) {

    internal lateinit var scrollState: ScrollState

    private val headerPositions: MutableMap<String, Int> = mutableMapOf()
    private val linkDefinitions = mutableMapOf<String, LinkDestination>()
    internal var event = mutableStateOf<MarkdownInternEvent?>(null)

    private var linkIdentifierId: Int = 0

    fun addLinkDefinition(astNode: ASTNode, content: String) {
        val label =
            astNode.children.find { it.type == MarkdownElementTypes.LINK_LABEL }?.getTextInNode(content) ?: return
        val destination =
            astNode.children.find { it.type == MarkdownElementTypes.LINK_DESTINATION }?.getTextInNode(content) ?: return
        val title = astNode.children.find { it.type == MarkdownElementTypes.LINK_TITLE }?.getTextInNode(content)
        linkDefinitions[label.toString()] =
            LinkDestination(MarkdownLinkType.Reference, destination.toString(), title?.toString())
        println("$label: ${linkDefinitions[label.toString()]}")
    }

    fun addLinkDestination(annotation: String, destination: LinkDestination) {
        linkDefinitions[annotation] = destination
        println("link: ${linkDefinitions[destination.link]}")
    }

    fun getLinkDestination(key: String): LinkDestination? {
        return linkDefinitions[key]
    }

    fun getLinkIdentifier(annotation: String): String {
        return "$${linkIdentifierId++} $annotation"
    }


    fun addHeaderPosition(header: String, position: Int) {
        headerPositions[header] = position
    }

    suspend fun scrollToHeader(header: String) {
        headerPositions[header.removePrefix("#")]?.let {
            scrollState.animateScrollTo(it)
        }
    }
}

data class LinkDestination(
    val linkType: MarkdownLinkType,
    val link: String,
    val title: String?
)

enum class MarkdownLinkType(val tag: String) {
    Normal("MARKDOWN_LINK_NORMAL"),
    Reference("MARKDOWN_LINK_REFERENCE"),
    Anchor("MARKDOWN_LINK_ANCHOR"),
    AUTO("MARKDOWN_LINK_AUTO"),
    EMAIL("MARKDOWN_LINK_EMAIL"),
}

fun buildMarkdownState(
    onEvent: (MarkdownEvent) -> Unit = ::defaultOnEvent,
): MarkdownState {

    return MarkdownState(onEvent)
}

sealed interface MarkdownInternEvent {
    data class OnClickAnchor(val header: String) : MarkdownInternEvent
}

sealed interface MarkdownEvent {
    data class ClickLink(val defaultUriHandler: UriHandler, val link: String) : MarkdownEvent
    data class Email(val email: String) : MarkdownEvent
}

fun defaultOnEvent(event: MarkdownEvent) {
    when (event) {
        is MarkdownEvent.ClickLink -> {
            event.defaultUriHandler.openUri(event.link)
        }
        is MarkdownEvent.Email -> {
            // Do nothing
            // TODO 后续考虑实现默认的邮件发送
        }
    }
}
